package com.baobao.demo;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.squareup.okhttp.Response;

@RestController
@RequestMapping("/hystrix")
public class HystrixController {

	@Autowired
	private HystrixUtil hystrixUtil;

	@RequestMapping(value = "/firstHystrix", method = RequestMethod.GET)
	public String getHotelInfo(@RequestParam("id") int id, @RequestParam("name") String name) {
		String url = "http://localhost:8080/hotel/getHotelInfo?id=" + id + "&name=" + name;
		Response response = null;
		try {
			response = hystrixUtil.execute(url);
			if (response != null) {
				return response.body().string();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} finally {
			if (response != null && response.body() != null) {
				try {
					response.body().close();// 资源关闭
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return "获取酒店信息失败";
	}

}